package com.ourhours.redismq.demo.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ourhours.redismq.Message;
import com.ourhours.redismq.RedisMQMessageSender;
import com.ourhours.redismq.demo.message.MessageType;


@Controller
public class MessageTestController {
	@Autowired
	RedisMQMessageSender messageSender;
	
	@RequestMapping("/message/send")
	@ResponseBody
	public String sendMessage(String msg) {
		Long result = null;
		try {
			if(msg == null || msg == "") {
				msg = "NO MESSAGE";
			}
			result = messageSender.put(new Message(MessageType.DEMO_MESSAGE, msg));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if(result != null) {
			return "success:"+result;
		}else {
			return "fail";
		}
	}
	
	@RequestMapping("/message/groupSend")
	@ResponseBody
	public String groupSendMessage(String msg1,String msg2) {
		try {
			List<Message> messageList = new ArrayList<Message>();
			Message message1 = new Message(MessageType.DEMO_MESSAGE, msg1);
			messageList.add(message1);
			Message message2 = new Message("test", msg2);
			messageList.add(message2);
			messageSender.groupput(messageList);
			return "success";
		} catch (Exception e) {
			e.printStackTrace();
			return "fail";
		}
	}
	
	@RequestMapping("/test/send")
	@ResponseBody
	public String sendTestMessage(String msg) {
		Long result = null;
		try {
			if(msg == null || msg == "") {
				msg = "NO MESSAGE";
			}
			result = messageSender.put(new Message("test", msg));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if(result != null) {
			return "success:"+result;
		}else {
			return "fail";
		}
	}
	
	@RequestMapping("/test/sendDemo")
	@ResponseBody
	public String sendTestDemoMessage(String msg) {
		Long result = null;
		try {
			if(msg == null || msg == "") {
				msg = "NO MESSAGE";
			}
			result = messageSender.put(new Message("obj_test", msg));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if(result != null) {
			return "success:"+result;
		}else {
			return "fail";
		}
	}
}
